//
//  ColorArrayViewController.m
//  蓝松RGB演示
//
//  Created by sno on 15-2-4.
//  Copyright (c) 2015年 Texas Instruments. All rights reserved.
//

#import "ColorArrayViewController.h"
#import "QCheckBox.h"
#import "CmdData.h"
#import "BLEUtility.h"

#define kOneX 10
#define kOneY 100
#define kBtnWidth 60
#define kBtnHeight 60

@interface ColorArrayViewController ()

@end

@implementation ColorArrayViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor grayColor];
    // Do any additional setup after loading the view, typically from a nib.
    
    _colorArray=[[NSMutableArray alloc] init];
    _chkArray=[[NSMutableArray alloc] init];
    
      UILabel  *name=[[UILabel alloc]init];
                    name.frame=CGRectMake(0,60,320, 40);
        name.text=@"最大可选择10个颜色值";
    [self.view addSubview:name];
       
   //第一行.
   // [self createBtn:0 y:0 color:[UIColor redColor]];
    
    
    [self createBtn:0 y:0 color:[UIColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:1.000]];
    [self createBtn:1 y:0 color:[UIColor colorWithRed:0.0 green:1.0 blue:0.0 alpha:1.000]];
    [self createBtn:2 y:0 color:[UIColor colorWithRed:0.0 green:0.0 blue:1.0 alpha:1.000]];
    [self createBtn:3 y:0 color:[UIColor colorWithRed:1.0 green:0.84 blue:0.0 alpha:1.000]];
    
    [self createBtn:0 y:1 color:[UIColor colorWithRed:1.0 green:0.0 blue:1.0 alpha:1.000]];
    [self createBtn:1 y:1 color:[UIColor colorWithRed:0.93 green:0.5 blue:0.93 alpha:1.000]];
    [self createBtn:2 y:1 color:[UIColor colorWithRed:1.0 green:0.20 blue:0.2 alpha:1.000]];
    [self createBtn:3 y:1 color:[UIColor colorWithRed:0.0 green:1.0 blue:1.0 alpha:1.000]];
    //第三行
    
   
    [self createBtn:0 y:2 color:[UIColor colorWithRed:0.82 green:0.70 blue:0.54 alpha:1.000]];
    [self createBtn:1 y:2 color:[UIColor colorWithRed:0.33 green:0.41 blue:0.18 alpha:1.000]];
    [self createBtn:2 y:2 color:[UIColor colorWithRed:1.00 green:0.89 blue:0.76 alpha:1.000]];
    [self createBtn:3 y:2 color:[UIColor colorWithRed:0.52 green:0.87 blue:0.92 alpha:1.000]];
    //第四行
    [self createBtn:0 y:3 color:[UIColor colorWithRed:1.0 green:0.3 blue:0.5 alpha:1.000]];
    [self createBtn:1 y:3 color:[UIColor colorWithRed:0.2 green:0.6 blue:0.4 alpha:1.000]];
    [self createBtn:2 y:3 color:[UIColor colorWithRed:0.7 green:0.8 blue:0.5 alpha:1.000]];
    [self createBtn:3 y:3 color:[UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.000]];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
    btn.frame = CGRectMake(kOneX+100, kOneY+4*70, 80, 80);
    
    btn.backgroundColor=[UIColor blueColor];
    [btn setTitle:@"设置" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(doSetcolor:) forControlEvents:UIControlStateHighlighted];
    [self.view addSubview:btn];
    
}
- (void)doSetcolor:(UIButton *)sender
{
    if(_colorArray.count<=0)
    {
       [self.view makeToast2:@"没有选择颜色, 请选择颜色!"];
        return;
    }else if (_colorArray.count>10)
    {
        [self.view makeToast2:@"颜色最大10组,请删除多余的颜色值."];
        return;
    }
    
    NSData  *data=[CmdData getMoreRGBArray:_colorArray];
    if (data!=NULL)
    {
        if (data.length>20)
        {
            uint8_t *pTotal=(uint8_t *)[data bytes];
            uint8_t *p1=NULL;
            uint8_t *p2=NULL;
            p1=(uint8_t *)malloc(20);
            p2=(uint8_t *)malloc(data.length-20);
            if (p1!=NULL && p2!=NULL)
            {
                //p1 前二十个字节.
                memcpy(p1, pTotal, 20);
                 NSData *dataP1=[[NSData alloc]initWithBytes:p1 length:20];
                [BLEUtility writeData:dataP1];
                free(p1);
                p1=NULL;
                //p2 剩下的字节.
                memcpy(p2, &pTotal[20], data.length-20);
                NSData *dataP2=[[NSData alloc]initWithBytes:p2 length:data.length-20];
                 [BLEUtility writeData:dataP2];
                free(p2);
                p2=NULL;
            }
        }else
            [BLEUtility writeData:data];
    }
    [_colorArray removeAllObjects];
    //取消选中.
    for (int i=0; i<[_chkArray count];i++) {
        QCheckBox *checkbox=[_chkArray objectAtIndex:i];
        [checkbox setChecked:NO];
    }
  [self.view makeToast2:@"已发送"];
}
- (void)createBtn:(int)x y:(int)y color:(UIColor*)color
{
     QCheckBox *_check = [[QCheckBox alloc] initWithDelegate:self];
            _check.frame = CGRectMake(kOneX+x*(kBtnWidth+10), kOneY+y*(kBtnHeight+10), kBtnWidth,kBtnHeight);
            _check.backgroundColor=color;
          [self.view addSubview:_check];
    [_chkArray addObject:_check];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)didSelectedCheckBox:(QCheckBox *)checkbox checked:(BOOL)checked {
   // NSLog(@"did tap on CheckBox:%@ checked:%d", checkbox.titleLabel.text, checked);
    
    UIColor *color=checkbox.backgroundColor;
    if (checked) {
        [_colorArray addObject:color];
        
    }else{
        if ([_colorArray containsObject:color]) {
            [_colorArray removeObject:color];
        }
    }
    CGFloat R,G,B,A;
    [color getRed:&R green:&G blue:&B alpha:&A];
  //  NSLog(@"color R:%f,G:%f,B:%f",R,G,B);
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
